<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<style>.unseen_input{border:none;}</style>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.autocomplete.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.autocomplete.js"></script>
<script type="text/javascript">
Refresh = function(){
	var wid = $("#warehouse_id").val();
	//jq的自动完成
	$(".ajax_product").autocomplete("<?=url('Storage::Purchase/ProductInfoAjax')?>&warehouse_id="+wid).result(function(event, data, formatted){
		if (data[0])
		{
			var container = $(this).parent().parent().parent();
			container.find("input.product_number").val(data['number']);
			container.find("input.available_quantity").val(data['available_quantity']);
			container.find("input[name$='product_basic_id]']").val(data['id']);
			container.find("input[name$='actual_price]']").val(data['purchase_price']);
			if (data['preferred_provider_id'])
			{
				container.find("input.actual_provider_id").val(data['preferred_provider_id']);
				container.find("input.ajax_provider").val(data['preferred_provider_id']+data['preferred_provider_name']);
			}
		}
		$("input.ajax_product").focus(function (){if ($(this).val().length*1.6 > 32){$(this).attr("size",$(this).val().length*1.8);}});
		$("input.ajax_product").keyup(function (){if ($(this).val().length*1.6 > 32){$(this).attr("size",$(this).val().length*1.8);}});
	});
	$(".ajax_provider").autocomplete("<?=url('Storage::Purchase/Provider')?>").result(function(event, data, formatted){
		if (data[0])
		{
			var container = $(this).parent().parent().parent();
			container.find("input.actual_provider_id").val(data['id']);
		}
	});
};
$(function(){
	//日期
	$(".datepicker").datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
	//删除按钮
	$(".delete").live("click", function(){
		$(this).parent().parent().parent().parent().remove();
	});
	$("#warehouse_id").change(function(){
		$("#item_list").children().remove();
	});
	var i = 0 ;
	$("#add_item").click(function(){
		var _html = "<table width='100%' class='list_table'>";
		_html += "<tr><td align='left' colspan='5'><span>产品编号</span>";
		_html += " <input type='text' size='6' disabled='disabled' class='product_number' />";
		_html += " <input type='text' size='85' class='ajax_product' />";
		_html += "<input type='hidden' name='item[new_" + i + "][product_basic_id]' />";
		_html += " 可用库存 <input type='text' size='3' class='available_quantity' style='border:none' /></td></tr>"
		_html += "<tr><td width='130'>数量 <input type='text' size='5' name='item[new_" + i + "][actual_quantity]' /></td>";
		_html += "<td width='130'>价格 <input type='text' size='5' name='item[new_" + i + "][actual_price]' /></td>";
		_html += "<td width='300'>供应商 <input type='text' size='4' disabled='disabled' class='actual_provider_id' /> <input type='text' class='ajax_provider' /> <input type='hidden' class='actual_provider_id' name='item[new_" + i + "][actual_provider_id]' /></td>";
		_html += "<td>备注 <input type='text' size='50' name='item[new_" + i + "][notes]' /></td>";
		_html += "<td><a class='delete'>删除</a></td></tr></table>";
		$("#item_list").append(_html);
		Refresh();
		i++;
	});
	
	//保存采购单
	$("#add_save").click(function(){
		var can_save = 1;
		//检查item产品
		$("#item_list").find("input[name$='product_basic_id]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item产品编号");
				return false;
			}
		});
		//检查Item数量
		$("#item_list").find("input[name$='actual_quantity]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item数量");
				return false;
			}
		});
		//检查item价格
		$("#item_list").find("input[name$='actual_price]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item价格");
				return false;
			}
		});
		//检查item供应商
		$("#item_list").find("input[name$='actual_provider_id]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item供应商");
				return false;
			}
		});
		//检测是否有重复SKU
		var icp = $("input.product_number"),repeat = false;
		icp.each(function(){
			if (icp.filter('[value="' + this.value + '"]').size()>1)
			{
				repeat = this.value;
			}
		});
		if (repeat !== false) 
		{
			if(!confirm('有重复的产品：' + repeat + '！是否继续操作？'))
			{
				return false;
			}
		}
		//检查周期结款下供应商是否一致
		var icps = $(".actual_provider_id"),repeats = false;
		if($("#purchase_order_payment_type").val()==1)
		{
			icps.each(function(){
				if (icps.filter('[value !="' + this.value + '"]').size()>=1)
				{
					repeats = this.value;
				}
			});
			if (repeats !== false) 
			{
				alert('周期结款下供应商需要一致');
				return false;
			}
		}
		if (can_save)
		{
			$("form").submit();
			$(this).attr('disabled', true);
		}
	});
	Refresh();
});
</script>
<form method="post" action="<?=url('Storage::Purchase/DrawbackCreateSave')?>">
	<table class="form_table">
		<tr>
			<th>日期</th>
			<td><input type="text" name="purchase_time" value="<?=CURRENT_DATETIME?>" class="datepicker" /></td>
			<th>经手人员</th>
			<td>
				<select name="purchase_user_id">
					<? foreach ($user as $u):?>
					<option value="<?=$u['id']?>" <?if ($u['id'] == CURRENT_USER_ID):?> selected="selected" <?endif;?>><?=$u['name']?></option>
					<? endforeach;?>
				</select>
			</td>
		</tr>
		<tr>
			<th>结款类型</th>
			<td>
				<select name="purchase_order_payment_type" id="purchase_order_payment_type">
					<? foreach (Q::ini('custom_flag/purchase_order_payment_type') as $pt):?>
					<option value="<?=$pt['value']?>" <?if (isset($order['payment_type']) == $pt['value'])echo "selected='selected'" ?>><?=$pt['name']?></option>
					<? endforeach;?>
				</select>
			</td>
			<th>所在仓库</th>
			<td>
				<select name="warehouse_id" id="warehouse_id">
					<?foreach ($warehouse as $w):?>
					<option value="<?=$w['id']?>" <?if (isset($order['warehouse_id']) == $w['id'])echo "selected='selected'"?>><?=$w['name']?></option>
					<?endforeach;?>
				</select>
			</td>
		</tr>
		<tr>
			<th>备注</th>
			<td colspan="3"><textarea rows="3" cols="60" name="notes"></textarea></td>
		</tr>
	</table>
<div id="item_list"></div>
<div align="left"><a id="add_item">添加产品</a></div>
<div align="center"><input type="button" class="button" id="add_save" value="保存退货单" /><input type="hidden" name="<?=Q::ini('unique_token_name');?>" value="<?=Helper_BSS_Normal::genToken();?>"/></div>
</form>
<?php $this->_endblock();?>